package org.jeecg.modules.awards.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.awards.entity.Awards;
import org.jeecg.modules.awards.entity.vo.AwardsVO;
import org.jeecg.modules.awards.mapper.AwardsMapper;
import org.jeecg.modules.awards.service.AwardsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.prizeform.service.PrizeFormService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * @author liujiamiao
 * @since 2020-11-05
 */
@Service
public class AwardsServiceImpl extends ServiceImpl<AwardsMapper, Awards> implements AwardsService {

    @Autowired
    private PrizeFormService prizeFormService;

    private int count;
    /**
     * 分页查询
     * @param page
     * @param size
     * @param basicId
     * @return
     */
    @Override
    public IPage<Awards> pageSelect(Integer page, Integer size, Integer basicId) {
        Page<Awards> awardsPage = new Page<>(page,size);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("basic_id",basicId);
        IPage<Awards> ipage = this.page(awardsPage, wrapper);
        count = (page-1)*size;
        List<Awards> collect = ipage.getRecords().stream().map(entity -> {
            AwardsVO awardsVO = new AwardsVO();
            BeanUtils.copyProperties(entity, awardsVO);
            awardsVO.setPrizeFormString(prizeFormService.getById(entity.getPrizeForm()).getName());
            awardsVO.setSerialNumber(++count);
            awardsVO.setPrizeDateString(DateUtils.formatDate(entity.getPrizeDate(),"yyyy-MM-dd"));
            return awardsVO;
        }).collect(Collectors.toList());
        ipage.setRecords(collect);
        return ipage;
    }



}
